package com.qzdsoft.erpcloud.service.pu.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.pu.PurchaseApply;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseApplyMapper;
import com.qzdsoft.erpcloud.mapper.pu.vi.PurchaseApplyDetailInfoMapper;
import com.qzdsoft.erpcloud.mapper.pu.vi.PurchaseApplyQueryMapper;
import com.qzdsoft.erpcloud.service.pu.PurchaseApplyService;
import com.qzdsoft.erpcloud.vo.pu.PurchaseApplyDetailInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseApplyDetailQueryInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseApplyInfo;
import com.qzdsoft.utils.entityUtil.UtilsEntity;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
/**
 * 
 * 描述：采购申请单
 *
 * <p>detailed comment
 * @author ZhangHao Sep 29, 2017
 * @see
 * @since 1.0
 */
@Service 
public class PurchaseApplyServiceImpl implements PurchaseApplyService {
	
    private static final Logger logger = LoggerFactory.getLogger(PurchaseApplyServiceImpl.class);
	@Autowired
	PurchaseApplyMapper tPurchaseApplyEnMapper;
	@Autowired
	PurchaseApplyQueryMapper tPurchaseApplyViEnMapper;
	@Autowired
	PurchaseApplyDetailInfoMapper purchaseApplyDetailInfoMapper;
	
	
	//获取请购单首页数据（表头）
	@Override
    public LayTableResponse<PurchaseApplyInfo> getHeaderData(int page,int limit) {
        PageHelper.startPage(page, limit);
        List<PurchaseApplyInfo> purchaseOrders = tPurchaseApplyViEnMapper.findAllApply();
        LayTableResponse<PurchaseApplyInfo> po = new LayTableResponse<PurchaseApplyInfo>(purchaseOrders);
        return po;
    }
	
	/**
	 * 请购单新增保存
	 * @throws Exception 
	 * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#add(com.qzdsoft.erpcloud.domain.pu.PurchaseApply)
	 */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<String> add(PurchaseApply apply){
        //设置新增时间
        apply.setCtime(new Date());
        //设置创建人ID
        apply.setCreatorId(1);
        //设置单据状态【未审】
        apply.setReviewStatus(false);
        //设置公司信息ID
        apply.setCompanyId(1);
        //返回当前保存成功的ID
        int i = this.tPurchaseApplyEnMapper.insert(apply);
        if(i>0) {
            return Response.success("请购单【"+apply.getBillNo()+"】表头新增成功！！！");
        }else {
            return new Response<>(ResultEnum.ERROR);
        }
    }
    
    /**
     * 表头更新保存
     * @throws Exception 
     * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#edit(com.qzdsoft.erpcloud.domain.pu.PurchaseApply)
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<String> edit(PurchaseApply apply) throws Exception{
        PurchaseApply _apply = this.tPurchaseApplyEnMapper.selectByPrimaryKey(apply.getId());
        if (_apply == null) {
            logger.error("编辑请购单，根据id:{}查询到的请购单为空", apply.getId());
            throw new RuntimeException("根据id查询到的请购单为空");
        }
        UtilsEntity.copyEntity(apply, _apply);
        //设置新增时间
        _apply.setModifyTime(new Date());
        //设置创建人ID
        _apply.setModifyId(1);
        //返回当前保存成功的ID
        this.tPurchaseApplyEnMapper.updateByPrimaryKey(_apply);
        return Response.success("请购单【"+apply.getBillNo()+"】表头修改成功！！！");
    }
    /**
     * 根据ID查询唯一一条数据（VI）
     * @param id
     * @return
     * @throws Exception 
     */
    private PurchaseApplyInfo findOneRecord(int id) throws Exception{
        List<PurchaseApplyInfo> list = this.tPurchaseApplyViEnMapper.find(id);
        if(list.size()>1)
            throw  new Exception("错误信息为:查询[请购单]数据返回结果集非有且仅有一条记录。请联系系统管理员！");
        return list.get(0);
    }
    /**
     * 根据公司信息 和 id 拿到当前选择行
     * @throws Exception 
     * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#findById(int, java.lang.Integer)
     */
    @Override
    public PurchaseApplyInfo findById(int companyid, Integer id) throws Exception{
        return this.findOneRecord(id);
    }
    /**
     * 采购申请单删除
     * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#headerdel(com.qzdsoft.erpcloud.domain.pu.PurchaseApply)
     */
    @Override
    public Response<String> headerdel(PurchaseApply apply){
        //TODO 还缺少  删除明细
        this.tPurchaseApplyEnMapper.deleteByPrimaryKey(apply.getId());
        return Response.success("删除成功！！");
    }
    
    /**
     * 采购申请单  审核
     * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#review(java.lang.Integer)
     */
    @Override
    public Response<String> review(Integer id){
        PurchaseApply _apply = this.tPurchaseApplyEnMapper.selectByPrimaryKey(id);
        //判断单据的审核状态
        if(!_apply.getReviewStatus()) {
            //状态
            _apply.setReviewStatus(true);
            //修改人
            _apply.setModifyId(1);
            //修改时间
            _apply.setModifyTime(new Date());
            //审核人
            _apply.setReviewId(1);
            //审核时间
            _apply.setReviewTime(new Date());
            this.tPurchaseApplyEnMapper.updateByPrimaryKeySelective(_apply);
            return Response.success("单据【"+_apply.getBillNo()+"】审核成功！！");
        }else{
            return Response.error("该单据不是未审核状态，不能审核操作！");
        }
   }
   /**
    * 单据弃审
    * @see com.qzdsoft.erpcloud.service.pu.PurchaseApplyService#cancleReview(java.lang.Integer)
    */
    @Override
    public Response<String> cancleReview(Integer id){
        PurchaseApply _apply = this.tPurchaseApplyEnMapper.selectByPrimaryKey(id);
        //判断单据的审核状态
        if(_apply.getReviewStatus()) {
            //状态
            _apply.setReviewStatus(false);
            //修改人
            _apply.setModifyId(1);
            //修改时间
            _apply.setModifyTime(new Date());
            //审核人
            _apply.setReviewId(null);
            //审核时间
            _apply.setReviewTime(null);
            this.tPurchaseApplyEnMapper.updateByPrimaryKeySelective(_apply);
            return Response.success("单据【"+_apply.getBillNo()+"】弃审成功！！");
        }else{
            return Response.error("该单据已弃审，不能重复执行弃审操作！");
        }
    }

    @Override
    public List<TypeInfo> queryBillNo(Integer companyid)
    {
        return tPurchaseApplyViEnMapper.selectBillno(companyid);
    }
    
    @Override
    public List<PurchaseApplyDetailInfo> selectDetailByBillId(
            PurchaseApplyDetailQueryInfo queryinfo)
    {
//        return purchaseApplyDetailInfoMapper.selectById(info);;
        return null;
    }
    
}
